<template>
  <a-modal
    title="新增字段配置"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="表名称"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-select style="width: 100%" placeholder="请选择表名称" v-decorator="['tableName', {rules: [{ required: true, message: '请选择表名称！' }]}]">
                <a-select-option v-for="(item,index) in tableNameData" :key="index" :value="item.tableName">{{ item.tableComment }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24">
            <a-form-item
              label="字段索引"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              extra="字段英文名称如：name、create_user"
              has-feedback
            >
              <a-input placeholder="请输入字段索引" v-decorator="['fieldIndex', {rules: [{required: true, message: '请输入字段索引！'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="字段名称"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              has-feedback
            >
              <a-input placeholder="请输入字段名称" v-decorator="['fieldTitle', {rules: [{required: true, message: '请输入字段名称！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="字段类型"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-select style="width: 100%" placeholder="请选择字段类型" v-decorator="['fieldType', {rules: [{ required: true, message: '请选择字段类型！' }]}]">
                <a-select-option v-for="(item,index) in fieldTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-form-item
          label="备注信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea :rows="3" placeholder="请输入备注信息" v-decorator="['remarks']" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { fieldConfigAdd } from '@/api/modular/main/fieldconfig/fieldConfigManage'
  import { codeGenerateInformationList } from '@/api/modular/gen/codeGenerateManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        },

        labelCol_T: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol_T: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        fieldTypeData: [],
        tableNameData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      async add (record) {
        this.visible = true
        this.fieldTypeData = this.$options.filters['dictData']('field_type')

        await codeGenerateInformationList().then((res) => {
          this.tableNameData = res.data
        })
        setTimeout(() => {
          this.form.setFieldsValue({
            fieldType: this.fieldTypeData.length !== 0 ? this.fieldTypeData[0].code : '',
            tableName: this.tableNameData.length !== 0 ? this.tableNameData[1].tableName : ''
          })
        }, 100)
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            fieldConfigAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
